-- Lua Battle System - 技能效果实现

local Logger = require("utils.logger")
local StatusEffects = require("characters.status_effects")

local SkillEffects = {}

-- 技能效果基类
local SkillEffect = {
    id = "",
    name = "",
    description = "",
    type = "", -- damage, healing, status, buff, debuff
    
    -- 效果参数
    params = {},
    
    -- 效果触发条件
    trigger_conditions = {
        on_cast = true,
        on_hit = false,
        on_crit = false,
        on_kill = false
    }
}

-- 技能效果构造函数
function SkillEffect:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end

-- 触发技能效果
function SkillEffect:trigger(caster, target, skill_result)
    if not self:check_trigger_conditions(skill_result) then
        return false
    end
    
    return self:apply(caster, target, skill_result)
end

-- 检查触发条件
function SkillEffect:check_trigger_conditions(skill_result)
    if skill_result.is_critical and not self.trigger_conditions.on_crit then
        return false
    end
    
    if skill_result.damage > 0 and not self.trigger_conditions.on_hit then
        return false
    end
    
    if skill_result.target_is_killed and not self.trigger_conditions.on_kill then
        return false
    end
    
    return true
end

-- 应用技能效果
function SkillEffect:apply(caster, target, skill_result)
    -- 默认空实现，子类重写
    return false
end

-- 具体技能效果实现

-- 伤害效果
local DamageEffect = SkillEffect:new({
    id = "damage",
    name = "伤害效果",
    description = "对目标造成伤害",
    type = "damage",
    
    params = {
        base_damage = 0,
        multiplier = 1.0,
        damage_type = "physical" -- physical, magic, true
    },
    
    trigger_conditions = {
        on_cast = true,
        on_hit = true,
        on_crit = false,
        on_kill = false
    }
})

function DamageEffect:apply(caster, target, skill_result)
    local damage = self.params.base_damage
    
    if self.params.damage_type == "physical" then
        damage = damage + caster.attack * self.params.multiplier
    elseif self.params.damage_type == "magic" then
        damage = damage + caster.magic_attack * self.params.multiplier
    end
    
    -- 添加到技能结果中
    skill_result.damage = (skill_result.damage or 0) + damage
    Logger.debug("应用伤害效果: " .. damage .. " 点 " .. self.params.damage_type .. " 伤害")
    
    return true
end

-- 治疗效果
local HealingEffect = SkillEffect:new({
    id = "healing",
    name = "治疗效果",
    description = "对目标进行治疗",
    type = "healing",
    
    params = {
        base_healing = 0,
        multiplier = 1.0,
        healing_type = "magic" -- magic, physical, true
    },
    
    trigger_conditions = {
        on_cast = true,
        on_hit = false,
        on_crit = false,
        on_kill = false
    }
})

function HealingEffect:apply(caster, target, skill_result)
    local healing = self.params.base_healing
    
    if self.params.healing_type == "magic" then
        healing = healing + caster.magic_attack * self.params.multiplier
    elseif self.params.healing_type == "physical" then
        healing = healing + caster.attack * self.params.multiplier
    end
    
    -- 添加到技能结果中
    skill_result.healing = (skill_result.healing or 0) + healing
    Logger.debug("应用治疗效果: " .. healing .. " 点 " .. self.params.healing_type .. " 治疗")
    
    return true
end

-- 状态效果施加器
local StatusEffectApplier = SkillEffect:new({
    id = "status_effect",
    name = "状态效果施加器",
    description = "对目标施加状态效果",
    type = "status",
    
    params = {
        effect_id = "",
        effect_params = {},
        duration = 0,
        chance = 1.0
    },
    
    trigger_conditions = {
        on_cast = false,
        on_hit = true,
        on_crit = false,
        on_kill = false
    }
})

function StatusEffectApplier:apply(caster, target, skill_result)
    -- 检查触发概率
    if math.random() > self.params.chance then
        return false
    end
    
    -- 创建状态效果
    local effect = StatusEffects.create_effect(self.params.effect_id, {
        duration = self.params.duration,
        caster = caster.id,
        skill_id = skill_result.skill_id
    })
    
    if not effect then
        Logger.error("无法创建状态效果: " .. self.params.effect_id)
        return false
    end
    
    -- 应用状态效果
    target:add_status_effect(effect)
    
    -- 添加到技能结果中
    skill_result.status_effects_applied = skill_result.status_effects_applied or {}
    table.insert(skill_result.status_effects_applied, {
        target = target.id,
        effect = effect.id
    })
    
    Logger.debug("应用状态效果: " .. effect.name .. " 到 " .. target.name)
    
    return true
end

-- 暴击伤害提升效果
local CritDamageBoost = SkillEffect:new({
    id = "crit_damage_boost",
    name = "暴击伤害提升",
    description = "提升技能的暴击伤害",
    type = "buff",
    
    params = {
        crit_multiplier_bonus = 0.5
    },
    
    trigger_conditions = {
        on_cast = true,
        on_hit = false,
        on_crit = true,
        on_kill = false
    }
})

function CritDamageBoost:apply(caster, target, skill_result)
    if skill_result.is_critical then
        local bonus_damage = skill_result.damage * self.params.crit_multiplier_bonus
        skill_result.damage = skill_result.damage + bonus_damage
        Logger.debug("应用暴击伤害提升: " .. bonus_damage .. " 点额外伤害")
        return true
    end
    return false
end

-- 生命偷取效果
local LifestealEffect = SkillEffect:new({
    id = "lifesteal",
    name = "生命偷取",
    description = "根据造成的伤害恢复生命值",
    type = "healing",
    
    params = {
        lifesteal_percentage = 0.1 -- 10% 生命偷取
    },
    
    trigger_conditions = {
        on_cast = false,
        on_hit = true,
        on_crit = false,
        on_kill = false
    }
})

function LifestealEffect:apply(caster, target, skill_result)
    if skill_result.damage > 0 then
        local lifesteal = skill_result.damage * self.params.lifesteal_percentage
        caster:heal(lifesteal)
        
        -- 添加到技能结果中
        skill_result.lifesteal = (skill_result.lifesteal or 0) + lifesteal
        Logger.debug("应用生命偷取: " .. lifesteal .. " 点生命值恢复")
        return true
    end
    return false
end

-- 魔法值偷取效果
local ManaStealEffect = SkillEffect:new({
    id = "mana_steal",
    name = "魔法值偷取",
    description = "根据造成的伤害恢复魔法值",
    type = "support",
    
    params = {
        mana_steal_percentage = 0.05 -- 5% 魔法值偷取
    },
    
    trigger_conditions = {
        on_cast = false,
        on_hit = true,
        on_crit = false,
        on_kill = false
    }
})

function ManaStealEffect:apply(caster, target, skill_result)
    if skill_result.damage > 0 then
        local mana_steal = skill_result.damage * self.params.mana_steal_percentage
        mana_steal = math.floor(mana_steal)
        
        -- 恢复魔法值
        caster.mp = math.min(caster.max_mp, caster.mp + mana_steal)
        
        -- 添加到技能结果中
        skill_result.mana_steal = (skill_result.mana_steal or 0) + mana_steal
        Logger.debug("应用魔法值偷取: " .. mana_steal .. " 点魔法值恢复")
        return true
    end
    return false
end

-- 溅射伤害效果
local SplashDamageEffect = SkillEffect:new({
    id = "splash_damage",
    name = "溅射伤害",
    description = "对目标周围的敌人造成伤害",
    type = "damage",
    
    params = {
        splash_percentage = 0.5, -- 50% 溅射伤害
        splash_range = 1 -- 溅射范围
    },
    
    trigger_conditions = {
        on_cast = false,
        on_hit = true,
        on_crit = false,
        on_kill = false
    }
})

function SplashDamageEffect:apply(caster, target, skill_result)
    if skill_result.damage > 0 then
        local splash_damage = skill_result.damage * self.params.splash_percentage
        
        -- TODO: 实现溅射伤害逻辑，对周围敌人造成伤害
        -- 这里需要获取战斗场景中的其他敌人，暂时简化处理
        
        -- 添加到技能结果中
        skill_result.splash_damage = (skill_result.splash_damage or 0) + splash_damage
        Logger.debug("应用溅射伤害: " .. splash_damage .. " 点范围伤害")
        return true
    end
    return false
end

-- 技能效果工厂
function SkillEffects.create_effect(effect_id, params)
    params = params or {}
    
    local effect_map = {
        damage = DamageEffect,
        healing = HealingEffect,
        status_effect = StatusEffectApplier,
        crit_damage_boost = CritDamageBoost,
        lifesteal = LifestealEffect,
        mana_steal = ManaStealEffect,
        splash_damage = SplashDamageEffect
    }
    
    local effect_class = effect_map[effect_id]
    if not effect_class then
        Logger.error("未知的技能效果: " .. effect_id)
        return nil
    end
    
    local effect = effect_class:new(params)
    effect.id = effect_id
    return effect
end

-- 导出模块
return SkillEffects